package com.p7.notepad.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.p7.notepad.R;
import com.p7.notepad.adapter.NotepadAdapter;
import com.p7.notepad.app.BaseActivity;
import com.p7.notepad.bean.Notepad;
import com.p7.notepad.network.APIManager;
import com.p7.notepad.network.exception.ExceptionHandle;
import com.p7.notepad.network.listener.RequestListener;
import com.p7.notepad.tools.Storage;
import com.tools.s7.recycler.DividerItemDecoration;
import com.tools.s7.recycler.listener.Recycler;
import com.tools.s7.recycler.utils.LoadMoreHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class SearchActivity extends BaseActivity implements LoadMoreHelper.LoadDataListener, Recycler.OnItemClickListener<Notepad.DataBean> {

    @BindView(R.id.search_text)
    EditText mText;
    @BindView(R.id.search_data)
    RecyclerView mData;
//    @BindView(R.id.search_srl)
//    SwipeRefreshLayout mSrl;

    private LoadMoreHelper mHelper;

    @Override
    public int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        mHelper = new LoadMoreHelper.Builder<Notepad.DataBean>()
                .setPageSize(100)
//                .setSwipeRefresh(mSrl)
                .setRecycler(mData, new LinearLayoutManager(this), new DividerItemDecoration(this, 1, 0xFFDFDFDF))
                .setAdapter(new NotepadAdapter(), false)
                .setLoadData(this)
                .setItemClick(this)
                .build();

        mText.addTextChangedListener(watcher);
    }

    TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (!TextUtils.isEmpty(editable)) {
                text = editable.toString();
                mHelper.loadFirstPage();
            } else {
                text = "";
                mHelper.loadFirstPage();
            }
        }
    };

    @OnClick({R.id.search_quit, R.id.search_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.search_quit:
                finish();
                break;
            case R.id.search_btn:
                isLoad();
                break;
        }
    }

    String text;
    private void isLoad() {
        text = mText.getText().toString();
        if (TextUtils.isEmpty(text)) {
            Toast.makeText(this, "搜索内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        mHelper.loadFirstPage();
    }

    @Override
    public void onLoadData(final int i) {
        Map<String, String> parameters = new HashMap<>();
        parameters.put("search", text);
        parameters.put("userId", String.valueOf(Storage.getId()));
        APIManager.onPost("notepad/findNotepad", parameters, new RequestListener() {
            @Override
            public void onSuccess(String s) {
                Gson gson = new Gson();
                Notepad notepad = gson.fromJson(s, Notepad.class);
                if (notepad.getCode() == 1000) {
                    if (notepad.getData() == null) {
                        mHelper.notifyNull();
                    } else {
                        mHelper.notifyAdapter(notepad.getData());
                    }

                } else if (notepad.getCode() == 2001) {
                    startActivity(LoginActivity.class);
                } else {
                    if (i == 1) {
                        mHelper.notifyNull();
                    } else {
                        mHelper.notifyError();
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponeThrowable error) {
                mHelper.notifyError();
            }
        });
    }

    @Override
    public void onItemClick(int i, Notepad.DataBean dataBean) {
        Bundle bundle = new Bundle();
        bundle.putString("id", String.valueOf(dataBean.getId()));
        startActivity(NotepadDetailsActivity.class, bundle);
    }
}
